/*
 *  Unified  KeyCode for JQuery 
 *	Version : 1.0
 *	 
 *  Date	: April 03 2010
 *	Author  : DEMONTE Jean-Baptiste (firejocker)
 *	Contact : jbdemonte@gmail.com
 * 	Licence : GPL Version 3 - http://www.gnu.org/licenses/gpl.html
 */
 
jQuery.keyboard = {
  qwerty: false,
  azerty: true
};

jQuery.browserKeycodes      = null;
jQuery.browserShiftKeycodes = null;

if (!$.browser.opera){
  if (jQuery.keyboard.azerty){
    jQuery.browserShiftKeycodes = {38:49,233:50,34:51,39:52,40:53,45:54,232:55,95:56,231:57,224:48,41:176,61:43};
  } else {
    jQuery.browserShiftKeycodes = {33:49,64:50,35:51,36:52,37:53,94:54,38:55,42:56,40:57,41:48};
  }
}

if ($.browser.mozilla){
  jQuery.browserKeycodes = {96:48,97:49,98:50,99:51,100:52,101:53,102:54,103:55,104:56,105:57,111:47,106:42,109:45,107:43,49:38,50:233,51:34,52:39,53:40,54:45,55:232,56:95,57:231,48:224,219:41,107:61,59:94,59:36,192:249,220:42,223:33,191:58,190:59,188:44,226:60};
  
}else if ($.browser.msie){
  jQuery.browserKeycodes = {96:48,97:49,98:50,99:51,100:52,101:53,102:54,103:55,104:56,105:57,111:47,106:42,109:45,107:43,49:38,50:233,51:34,52:39,53:40,54:45,55:232,56:95,57:231,48:224,219:41,187:61,186:94,186:36,192:249,220:42,223:33,191:58,190:59,188:44,226:60};
}else if ($.browser.webkit || $.browser.safari){
  jQuery.browserKeycodes = {96:48,97:49,98:50,99:51,100:52,101:53,102:54,103:55,104:56,105:57,111:47,106:42,109:45,107:43,49:38,50:233,51:34,52:39,53:40,54:45,55:232,56:95,57:231,48:224,219:41,187:61,186:94,186:36,192:249,220:42,188:44,190:59,191:58,223:33,226:60};
}

jQuery.each( ("keydown keypress keyup").split(" "), function( i, name ) {
  jQuery.fn[ name ] = function( fn ) {
    if ( typeof(fn) == "function" ){
      var user_fn = fn;
      var fn = function(e){
        var kc = e.keyCode || e.charCode || e.which;
        if (e.type != "keypress"){
          if (e.shiftKey){
            if (jQuery.browserShiftKeycodes && jQuery.browserShiftKeycodes[kc]) kc = jQuery.browserShiftKeycodes[kc];
          } else {
            if (jQuery.browserKeycodes && jQuery.browserKeycodes[kc]) kc = jQuery.browserKeycodes[kc];
          }
        }
        e.unifiedKeyCode = kc;
        user_fn(e);
      }
    }
  	return fn ? this.bind( name, fn ) : this.trigger( name );
  };
});